﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ABCTile.Models;
using ABCTile.DAL;
using ABCTile.ViewModel;

namespace ABCTile.Controllers
{ 
    public class ProposalController : Controller
    {
        private ABCDbContext db = new ABCDbContext();

        //
        // GET: /Proposal/

        //public ViewResult Index()
        //{            
        //    return View(db.Proposals.ToList());
        //}
        //<div class="editor-label" style="text-align: right;">
/*********************/
        public ActionResult Index(Int32? id, Int32? ProposalID)
        {

            var viewModel = new EstimateProposalIndexData();            
            var zquery = "SELECT Estimate.* from Estimate LEFT OUTER JOIN Proposal ON Estimate.Id = Proposal.Id ";

            if (id != null)
            {
                zquery += "WHERE Estimate.Id = " + id;
            }
            viewModel.Estimates = db.Database.SqlQuery<Estimate>(zquery);

            if (id != null)
            {
                ViewBag.EstimateID = id.Value;
                //var z2query = "SELECT Proposal.* FROM Estimate INNER JOIN Proposal ON EstimateId = Proposal.Id and Estimate.Id =" + id;
                var z2query = "SELECT Proposal.* FROM Proposal WHERE Proposal.Id =" + id;
                //var z2query = "SELECT Proposal.* from Estimate LEFT OUTER JOIN Proposal ON Estimate.Id = Proposal.Id ";
                //if (id != null)
                //{
                //    z2query += "WHERE Estimate.Id = " + id;
                //}

                viewModel.Proposals = db.Database.SqlQuery<Proposal>(z2query);
            }

            if (id != null)
            {

                ViewBag.EstimateID = id.Value;
                var TotalQuery = "SELECT EstimateTotal AS Total FROM Estimate WHERE Id =" + id;
                EstimateTotal TotalTest = db.Database.SqlQuery<EstimateTotal>(TotalQuery).Single();
                ViewBag.zoutotal = TotalTest.Total;
            }

            if (id != null)
            {
                ViewBag.EstimateID = id.Value;
                var CountQuery = "SELECT COUNT(*) AS Count from Estimate LEFT OUTER JOIN Proposal ON Estimate.Id = Proposal.Id ";
                if (id != null)
                {
                    //CountQuery += "WHERE Proposal.Id = " + id;
                    CountQuery += "WHERE Estimate.Id = " + id;
                }

                EstimateCount CountTest = db.Database.SqlQuery<EstimateCount>(CountQuery).Single();
                ViewBag.proposalcount = CountTest.Count;
            }

            return View(viewModel);
        }
/*********************/
        //
        // GET: /Proposal/Details/5

        public ViewResult Details(int id)
        {
            Proposal proposal = db.Proposals.Find(id);
            return View(proposal);
        }

        //
        // GET: /Proposal/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Proposal/Create

        [HttpPost]
        public ActionResult Create(Proposal proposal)
        {
            if (ModelState.IsValid)
            {
                db.Proposals.Add(proposal);
                db.SaveChanges();
                return RedirectToAction("Index", "Estimate");  
            }

            return View(proposal);
        }
        
        //
        // GET: /Proposal/Edit/5
 
        public ActionResult Edit(int id)
        {
            Proposal proposal = db.Proposals.Find(id);
            return View(proposal);
        }

        //
        // POST: /Proposal/Edit/5

        [HttpPost]
        public ActionResult Edit(Proposal proposal)
        {
            if (ModelState.IsValid)
            {
                db.Entry(proposal).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(proposal);
        }

        //
        // GET: /Proposal/Delete/5
 
        public ActionResult Delete(int id)
        {
            Proposal proposal = db.Proposals.Find(id);
            return View(proposal);
        }

        //
        // POST: /Proposal/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Proposal proposal = db.Proposals.Find(id);
            db.Proposals.Remove(proposal);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}